import { VxeGridPropTypes } from "vxe-table";
import XEUtils from 'xe-utils';

export const columns: VxeGridPropTypes.Columns = [
  {type: 'radio', width: 50, fixed: 'left',},
  {type: 'seq', title: '序号', treeNode: false, width: 50, fixed: 'left',},
  {field: 'id', title: 'ID', width: 60, visible: false,},
  {field: 'code', title: '单据编号', sortable: false, visible: true, width: 150, fixed: 'left',},
  {field: 'name', title: '单据名称', sortable: false, visible: true, width: 100,},
  {field: 'workDate', title: '业务日期', sortable: true, visible: true, width: 140,},
  {field: 'verifier.name', title: '审核人', sortable: false, visible: true, width: 80,},
  {field: 'status', title: '状态', sortable: false, width: 80, 
  formatter: ({row,}) => {
    let tip = '未知'
    switch(row.status) {
      case 1: tip = '未审核'; break;
      case 2: tip = '已审核'; break;
      case 3: tip = '已关闭'; break;
      case 4: tip = '已作废'; break;
    }
    return tip
  }},
  {field: 'handler.name', title: '经手人', sortable: false, visible: false, width: 80,},
  {field: 'supplier.name', title: '供应商', sortable: false, visible: true, width: 120,},
  {field: 'customer.name', title: '客户', sortable: false, visible: true, width: 120,},
  {field: 'goodsList.name', title: '订单商品', sortable: false, visible: true, width: 120,
  formatter: ({row,}) => {
    if (row.goodsList) {
      return row.goodsList.map(e => e.goods.name).toString()
    }
    return 'goods ...';
  }},
  {field: 'qty', title: '商品数量', sortable: true, visible: true, width: 100,
  formatter: ({row,}) => {
    if (row.goodsList) {
      let qty = row.goodsList.reduce((pre, cur,) => pre + cur.qty, 0)
      return XEUtils.commafy(XEUtils.toNumber(qty), { digits: 1, });
    }
    return 0;
  }},
  {field: 'invtQtyTotal', title: '已入库', sortable: false, visible: true, width: 100,},
  {field: 'totalAmount', title: '金额', sortable: true, visible: true, width: 120,
  formatter: ({row,}) => {
    if (row.goodsList) {
      let totalAmount = row.goodsList.reduce((pre, cur,) => pre + (cur.qty * cur.price), 0)
      return XEUtils.commafy(XEUtils.toNumber(totalAmount), { digits: 2, });
    } 
    return 0;
  }},
  {field: 'memo', title: '备注', visible: true, sortable: false, width: 180,},
  {field: 'verifyDate', title: '审核日期', visible: false, sortable: false, width: 140,},
  {field: 'closeDate', title: '关闭日期', visible: false, sortable: false, width: 140,},
  {field: 'createdTime', title: '创建时间', visible: false, sortable: false, width: 140,},
  {field: 'updatedTime', title: '更新时间', visible: false, sortable: false, width: 140,},
];

export const pagerConfig: VxeGridPropTypes.PagerConfig = {
    total: 0,
    currentPage: 1,
    pageSize: 20,
    pageSizes: [20, 50, 100, 200, 500, 1000],
    layouts: ['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total'],
    pagerCount: 3,
    border: true,
    background: true,
    perfect: true,
};


